﻿/*
 * Copyright (c) Jason Paul Morley, 2010
 * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core
{
    /// <summary>
    /// All exceptions thrown by the components can be of this type, allowing easier identification
    /// </summary>
    public class Error : Exception
    {
        public Error(string component, string function, string msg)
            : base(component + "->" + function + ": " + msg)
        {
            Component = component;
            Function = function;
            Note = msg;
        }

        public string Component { get; set; }
        public string Function { get; set; }
        public string Note { get; set; }
    }

    /// <summary>
    /// Provides static debugging functionality, printing writing debug text
    /// </summary>
    public static class Debug
    {
        private static Dictionary<string, System.IO.StreamWriter> _spv_debugLogs = new Dictionary<string, System.IO.StreamWriter>();

        public static void Write(string component, string function, string msg)
        {
            System.IO.StreamWriter writer;
            if (!_spv_debugLogs.ContainsKey(component))
            {
                writer = new System.IO.StreamWriter(component + ".txt");
                _spv_debugLogs[component] = writer;
            }
            else
                writer = _spv_debugLogs[component];

            writer.WriteLine(function + "->" + msg);
            writer.Flush();
        }

        public static void Log(string component, string function, string msg)
        {
#if DEBUG
            Write(component, function, msg);
#endif
        }
    }
}
